<div class="card mb-4">
  <div class="card-header">
    <h5 class="card-title">
      <%= Spree.t(:general_settings) %>
    </h5>
  </div>
  <div class="card-body">
    <%= f.spree_text_field :url, placeholder: 'https://example.com/endpoint' %>
    <%= f.spree_check_box :active %>
  </div>
</div>
<div class="card mb-4">
  <div class="card-header">
    <h5 class="card-title">
      <%= Spree.t('admin.webhooks_subscribers.webhooks_events') %>
    </h5>
  </div>
  <div class="card-body" data-controller="reveal">
    <div class="form-group">
      <div>
        <div class="custom-control custom-radio mb-3">
          <%= radio_button_tag :subscribe_to_all_events, true,
                            subscribe_to_all_events?,
                            {
                              'data-action' => 'click->reveal#hide',
                              class: 'custom-control-input events' } %>
          <%= label_tag :subscribe_to_all_events_true, Spree.t('admin.webhooks_subscribers.subscribe_to_all_events'), class: 'custom-control-label' %>
        </div>
        <div class="custom-control custom-radio">
          <%= radio_button_tag :subscribe_to_all_events, false,
                            !subscribe_to_all_events?,
                            { 'data-action' => 'click->reveal#show',
                              class: 'custom-control-input events' } %>
          <%= label_tag :subscribe_to_all_events_false, Spree.t('admin.webhooks_subscribers.select_events'), class: 'custom-control-label' %>

          <div data-reveal-target="item" class="<%= subscribe_to_all_events? ? 'hidden' : '' %>">
            <div class="bg-light rounded-lg p-3 mt-3">
              <% @supported_events.each do |event| %>
                <div class="form-group">
                  <div class="custom-control custom-switch">
                    <% resource_name = event.first %>                        
                    <%= f.check_box resource_name, 
                      { class: 'events-checkbox custom-control-input',
                        checked: @webhooks_subscriber.subscriptions&.any? { |event| event.include? "#{resource_name}." } },
                      true, 
                      nil %>
                      
                    <%= f.label resource_name, class: 'custom-control-label w-100' do %> 
                      <%= Spree.t("#{resource_name.to_s.pluralize}") %>
                      <p class="gap-2 mb-0 mt-1">
                        <% event.last.each do |event| %>
                          <span class="badge badge-light">
                            <%= event %>
                          </span>
                        <% end %>
                      </p>
                    <% end %>
                  </div>
                </div>
              <% end %>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

